package com.sinodata.bsm.cicp.collector.mysql;

import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class MysqlDBCollector extends AbstractJDBCCollector {
    private final Logger logger = Logger.getLogger(MysqlDBCollector.class);
    private final Map<String, String> dbKeyMap = new HashMap<String, String>();

    public MysqlDBCollector() {
        MySQLUtil.initDbKeyMap(getDbServKeys(), getPropMeta(), dbKeyMap);
    }

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        Map<String, String> dbMap = collectStatusFromDb(ip);
        if (!dbMap.containsKey("character_set_system")) {
            dbMap.put("character_set_system", dbMap.get("character_set"));
        }

        List<ResPropertyValue> values = MySQLUtil.createPropValues(getDbKeyMap(), getPropMeta(), dbMap);
        dbMap.clear();
        return values;
    }

    /**
     * @param ip
     * @return
     * @throws CollectorException
     */
    private Map<String, String> collectStatusFromDb(String ip) throws CollectorException {
        String dbtype = getParamMap().get("DBType");
        String dbname = getParamMap().get("DBName");
        ResultSet rsVar = null;
        ResultSet rsStatus = null;
        Map<String, String> dbMap = new HashMap<String, String>();
        try {
            rsVar = this.getCachedResultSet(dbtype, ip, dbname, MySQLContants.VARIABLES);
            while (rsVar.next()) {
                dbMap.put(rsVar.getString(1).toLowerCase(), rsVar.getString(2) == null ? "" : rsVar.getString(2).trim().toLowerCase());
            }
            rsStatus = this.getCachedResultSet(dbtype, ip, dbname, MySQLContants.STATUS);

            while (rsStatus.next()) {
                dbMap.put(rsStatus.getString(1).toLowerCase(), rsStatus.getString(2) == null ? "" : rsStatus.getString(2).trim().toLowerCase());
            }
        } catch (Exception ex) {
            logger.error("Collect mysql(" + ip + ") information error", ex);
            throw new CollectorException("Collect mysql(" + ip + ") information error", ex);
        }

        return dbMap;
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "DBType", "DBName" };
    }

    @Override
    public String[] getPropMeta() {
        return new String[] { "VersionInfo", "InstallCatalogue", "OperationSystem", "CharacterSet", "RunningTime" };
    }

    private String[] getDbServKeys() {
        return new String[] { "version", "basedir", "version_compile_os", "character_set_system", "uptime" };
    }

    /**
     * @return the dbKeyMap
     */
    public Map<String, String> getDbKeyMap() {
        return dbKeyMap;
    }

    /**
     * @param dbKeyMap
     *            the dbKeyMap to set
     */
    public void setDbKeyMap(String propMeta, String dbKey) {
        this.dbKeyMap.put(propMeta, dbKey);
    }
}
